package com.aaa.controller;

import com.aaa.entity.Emp;
import com.aaa.service.EmpService;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 员工表(Emp)表控制层
 *
 * @author makejava
 * @since 2022-11-17 14:30:43
 */
@Slf4j
@RestController
@RequestMapping("emp")
@Api(tags = "员工的增删查改")
//@CrossOrigin
public class EmpController {
    /**
     * 服务对象
     */
    @Resource
    private EmpService empService;

    /**
     * 查询页面
     * 分页查询
     *
     * @param currentPage 当前页面
     * @param pageSize    页面大小
     * @return {@link Result}<{@link Page}<{@link Emp}>>
     */
    @GetMapping
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码,默认1", example = "1",
                    paramType = "query", dataType = "int", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数,默认10", example = "10",
                    paramType = "query", dataType = "int", required = false)
    })
    public Result<Page<Emp>> queryByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                         @RequestParam(defaultValue = "5") Integer pageSize) {
        Page<Emp> page = empService.page(new Page<>(currentPage, pageSize), null);
        return Result.success(page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条数据")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "员工ID", required = true, dataType = "int")
    )
    @GetMapping("{id}")
    public Result<Emp> queryById(@PathVariable("id") Long id) {
        return Result.success(empService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param emp 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("添加员工接口")
    public Result<Boolean> add(@RequestBody Emp emp) {
        boolean b = empService.save(emp);
        if (b) {
            return Result.success(true, "删除成功");
        }
        return Result.error(false, "删除失败");
    }

    /**
     * 编辑数据
     *
     * @param emp 实体
     * @return 编辑结果
     */
    @ApiOperation(value = "更新员工接口", notes = "测试requestBody")
    @PutMapping
    public Result<Boolean> edit(@RequestBody Emp emp) {
        log.info("emp = {}", emp);
        boolean b = empService.updateById(emp);
        if (b) {
            return Result.success(true, "删除成功");
        }
        return Result.error(false, "删除失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation("删除员工接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键员工ID", example = "55",
                    paramType = "path", dataType = "int", required = true)
    })
    @DeleteMapping
    public Result<Boolean> deleteById(Long id) {
        boolean b = empService.removeById(id);
        if (b) {
            return Result.success(true, "删除成功");
        }
        return Result.error(false, "删除失败");
    }

}

